package org.wyf.EveryPoint.common.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * @author lw
 */
public class ParameterRequestWrapper extends HttpServletRequestWrapper {

    private Map<String , String[]> params = new HashMap<>() ;


    public ParameterRequestWrapper(HttpServletRequest request) {
        super(request);
        //将参数表，赋予给当前Map以便于持有request中的参数
        this.params.putAll(request.getParameterMap());
    }

    public ParameterRequestWrapper(HttpServletRequest request , Map<String , Object> extendParams){
        this(request);
        addAllParameters(extendParams) ;
    }

    /**
     * 重写getParameter方法
     * @param name 参数名
     * @return 参数数值
     */
    @Override
    public String getParameter(String name) {
        String[] values = params.get(name) ;
        if (values == null){
            return null ;
        }
        return values[0] ;
    }

    @Override
    public String[] getParameterValues(String name){
        String[] values = params.get(name) ;
        if (values == null || values.length == 0){
            return null ;
        }
        return values ;
    }

    /**
     * 在获取所有的参数名,必须重写此方法，
     * 否则对象中参数值映射不上
     * @return
     */
    @Override
    public Enumeration<String> getParameterNames(){
        return new Vector(params.keySet()).elements() ;
    }

    public void addAllParameters(Map<String, Object> extendParams) {
        for (Map.Entry<String , Object> entry : extendParams.entrySet()) {
            addParameter(entry.getKey() , entry.getValue());
        }
    }

    public void addParameter(String key, Object value) {
        if (value != null){
            if (value instanceof String[]) {
                params.put(key , (String[])value);
            } else if (value instanceof String) {
                params.put(key , new String[]{(String) value});
            } else {
                params.put(key , new String[]{String.valueOf(value)});
            }
        }
    }

}
